package com.face.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.face.server.convert.Student2NormStu;
import com.face.server.entity.Auth;
import com.face.server.entity.Dormitory;
import com.face.server.entity.DormitoryBuild;
import com.face.server.entity.Student;
import com.face.server.repository.AuthRepository;
import com.face.server.repository.DormitoryBuildRepository;
import com.face.server.repository.DormitoryRepository;
import com.face.server.repository.StudentRepository;
import com.face.server.repository.exRepository.exStudentRepository;
import com.face.server.service.StudentService;
import com.face.server.service.UtileService;
import com.face.server.vo.NormStu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ryan on 2018/1/25.
 */
@Service
public class StudentServiceImpl implements StudentService {


    @Autowired
    DormitoryRepository dormitoryRepository;

    @Autowired
    DormitoryBuildRepository dormitoryBuildRepository;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    GridFsTemplate gridFsTemplate;

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private UtileService utileService;


    @Autowired
    private AuthRepository authRepository;


    @Autowired
    private exStudentRepository exStudentRepository;

    @Override
    public Student editInfo(String sid, MultipartFile proPhoto, String userName, String phone) throws IOException {
        Student student= studentRepository.findOne(sid);
        String proid=utileService.upFile(proPhoto);
        Auth auth=student.getAuth();
        auth.setUsername(userName);
        auth.setProPhoto(proid);
        auth=authRepository.save(auth);
        student.setAuth(auth);
        student.setPhone(phone);
        return studentRepository.save(student);
    }

    @Override
    public Student addStudent(String stuName, Boolean stuSex, Integer stuNum, String idNum, Integer buildNum, Integer dormNum,String className,String stuPhone) {
        Student student=new Student();
        student.setPhone(stuPhone);
        student.setName(stuName);
        student.setIdentity(idNum);
        student.setSex(stuSex);
        Auth auth=new Auth();
        auth.setUsername(stuNum.toString());
        auth.setPassword(idNum.substring(12));
        auth=authRepository.save(auth);
        student.setAuth(auth);
        student.setPoint(0);
        student.setClassName(className);
        student.setStudentNumber(stuNum);
        DormitoryBuild dormitoryBuild=dormitoryBuildRepository.findByBuildingNum(buildNum);
        student.setDormitoryBuild(dormitoryBuild);
        student.setDormitoryBuildNum(buildNum);
        student.setDormitoryNum(dormNum);
        Dormitory dormitory=dormitoryRepository.findByDormitoryBuildNumAndRoomNumber(buildNum,dormNum);
        List<String> list=new ArrayList<String>();
        if(dormitory.getStudentsName()==null){
            list.add(stuName);
            dormitory.setStudentsName(list);
        }else {
            list=dormitory.getStudentsName();
            list.add(stuName);
            dormitory.setStudentsName(list);
        }
        dormitoryRepository.save(dormitory);
        return studentRepository.save(student);
    }


    @Override
    public Page<NormStu> listStudents(PageRequest pageRequest, String key1, String key2) {
        if ((key1==null||key1.equals(""))&&(key2==null||key2.equals(""))){
            List<Student> students=studentRepository.findAll(pageRequest).getContent();
            List<NormStu> normStus= Student2NormStu.convert(students);
            Page<NormStu> normStuPage=new PageImpl<NormStu>(normStus,pageRequest,normStus.size());
            return normStuPage;
        }
        List<Student> students=exStudentRepository.listStudents(pageRequest,key1,key2);
        List<NormStu> normStus= Student2NormStu.convert(students);
        Page<NormStu> normStuPage=new PageImpl<NormStu>(normStus,pageRequest,normStus.size());
        return normStuPage;
    }

    @Override
    public Void deleteStudents(String sids) {
        JSONObject jsonObject = new JSONObject();
        if(sids!=null) {
            jsonObject = JSON.parseObject(sids);
            JSONArray jsonArray=jsonObject.getJSONArray("sids");
            jsonArray.stream().forEach(
                    e->studentRepository.delete(e.toString())
            );
        }
        exStudentRepository.deleteStudents();
        return null;
    }



}
